//index.js
//获取应用实例
const mdCode = require('../../utils/md5.js');
const desCode = require('../../utils/aesUtil.js');
const api_js = require("../../utils/api.js");
var app = getApp();

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),


    bar_Height: wx.getSystemInfoSync().statusBarHeight,

    isIphoneX_: app.globalData.isIphoneX,
    // 是否登录
    isLogin: false,


    errorPhone: false,
    errorName: false,
    errorDate: false,
    errorHouse: false,

    // 姓名
    name: '',
    // 手机号
    phone: '',
    // 日期
    date: '',
    // 会场
    house: -1,
    // 会场集合
    houseList2: [{ name: '服务质量', id: 20 }, { name: '服务品质', id: 24 }, { name: '服务速度', id: 25 }],

    remark: '',

    // 是否显示弹层
    activeVenueShow: false,
    input_bottom:0

  },




  // 姓名验证
  checkName(value) {
    if (value == '') {
      this.setData({
        errorName: true
      });
      return false;
    } else {
      this.setData({
        errorName: false
      });
      return true;
    }
  },

  // 号码验证(手机)
  checkPhoness(phone) {

    if (!/^1[34578]\d{9}$/.test(phone)) {
      this.setData({
        errorPhone: true
      });
      return false;
    } else {
      this.setData({
        errorPhone: false
      });
      return true;
    }
  },



  checkPhoness2(phone) {

    if (!/^1[34578]\d{9}$/.test(phone)) {

      if (phone.length == 0 || phone.length == 11) {
        this.setData({
          errorPhone: true
        });
        return false;
      }

    } else {
      this.setData({
        errorPhone: false
      });
      return true;
    }
  },




  // 日期验证
  checkDate(value) {
    if (value == '') {
      this.setData({
        errorDate: true
      });
      return false;
    } else {
      this.setData({
        errorDate: false
      });
      return true;
    }
  },


  // 会场验证
  checkHouse(value) {
    if (value == -1) {
      this.setData({
        errorHouse: true
      });
      return false;
    } else {
      this.setData({
        errorHouse: false
      });
      return true;
    }
  },



  // 赋值和判断(手机号)
  phoneFun(e) {
    this.setData({
      phone: e.detail.value.replace(/\s+/g, '')
    });
    this.checkPhoness2(this.data.phone);
    console.log(this.data.phone)
  },


  // 赋值和判断(姓名)
  nameFun(e) {
    this.setData({
      name: e.detail.value.replace(/\s+/g, '')
    });
    this.checkName(this.data.name);
    console.log(this.data.name)
  },


  // 日期组件
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    });
    this.checkDate(this.data.date);
    console.log(this.data.date)
  },

  // 会场组件
  // https://www.jb51.net/article/157716.htm
  bindHouseChange(e) {
    console.log('picker发送选择改变，索引值为', e.detail.value)
    console.log(e);
    this.setData({
      house: e.detail.value
    });
    this.checkHouse(this.data.house);

    console.log(this.data.houseList2[this.data.house].name, "999")
  },

  // 赋值和判断(手机号)
  remarkFun(e) {
    this.setData({
      remark: e.detail.value.replace(/\s+/g, '')
    });
    console.log(this.data.remark)
  },

  // 会场赋值
  activeVenueFun(e) {
    this.setData({
      house: e.currentTarget.dataset.index,
      activeVenueShow: false,
    });
    this.checkHouse(this.data.house);


  },

  // 显示会场弹层
  activeVenueShowFun() {
    this.applyListFun();
    this.setData({
      activeVenueShow: true,
    });
  },


  cancel() {
    this.setData({
      activeVenueShow: false,
    });
  },



  formSubmit() {

    this.checkPhoness(this.data.phone);
    this.checkName(this.data.name);
    this.checkHouse(this.data.house);
    console.log(this.data.phone, "this.data.phone")
    console.log(this.data.name, "this.data.name")
    console.log(this.data.house, "this.data.house")
    if (this.checkPhoness(this.data.phone) && this.checkName(this.data.name) && this.checkHouse(this.data.house)) {

      // (107)活动报名接口传参
      let name = this.data.name;
      let phone = this.data.phone;
      let participantstime = this.data.houseList2[this.data.house].starttime;
      let meetingplace = this.data.houseList2[this.data.house].id;
      let remark = this.data.remark;
      let createuserid = app.globalData.UserID;
      var wMsgID = desCode.wMsgID();
      var wParam = desCode.to3des(`name=${name}_phone=${phone}_participantstime=${participantstime}_meetingplace=${meetingplace}_remark=${remark}_createuserid=${createuserid}`);
      var md = mdCode.hexMD5('9999' + '107' + wMsgID + wParam + 'q1w2e3r4t5y6');
      const data = {
        wAgent: 9999,
        wAction: 107,
        wMsgID: wMsgID,
        wParam: wParam,
        wSign: md,
        wImei: 222,
        wVersion: 2,
        wRequestUserID: 4
      }
      api_js.postReq(data, (res) => {

        if (res.ReturnCode == 0) {

          console.log(res, "表单提交");

          let barcode = res.Data[0].barcodes[0].barcode;
          let barcodeimg = res.Data[0].barcodes[0].barcodeimg;
          let barstatus = res.Data[0].barcodes[0].barstatus;
          let name = res.Data[0].name;
          let meetingplace = res.Data[0].meetingplace;
          let participantstime = res.Data[0].participantstime.split(' ')[0];
          let logo = res.Data[0].logo;
          let description = res.Data[0].description;
          let remark = res.Data[0].remark;

          console.log(barcode, "barcode");
          console.log(barcodeimg, "barcodeimg");
          console.log(barstatus, "barstatus");
          console.log(name, "name");
          console.log(meetingplace, "meetingplace");
          console.log(participantstime, "participantstime");

          setTimeout(() => {

            wx.navigateTo({
              url: `../QRCode/QRCode?barcode=${barcode}&barcodeimg=${barcodeimg}&barstatus=${barstatus}&name=${name}&meetingplace=${meetingplace}&participantstime=${participantstime}&logo=${logo}&description=${description}&remark=${remark}`
            });

            // wx.navigateTo({
            //   url: `../QRCode/QRCode?barcode=${barcode}&barcodeimg=${barcodeimg}&barstatus=${barstatus}&name=${name}&meetingplace=${meetingplace}&participantstime=${participantstime}&logo=${logo}&description=${description}&remark=${remark}`
            // })


          }, 1000)
        } else if (res.ReturnCode == 401) {
          wx.showToast({
            title: '报名时间已过',
            icon: 'none',
            duration: 1500,
            mask: true,
          })
        } else if (res.ReturnCode == 400) {
          wx.showToast({
            title: '请不要重复报名哦！',
            icon: 'none',
            duration: 1500,
            mask: true,
          })
        }
      })

    } else {
    }


  },


  applyListFun() {

    let wMsgID = desCode.wMsgID();
    // let wParam = desCode.to3des();
    let wParam = '';
    let md = mdCode.hexMD5('8888' + '111' + wMsgID + wParam + 'q1w2e3r4t5y6');
    const data = {
      wAgent: 8888,
      wAction: 111,
      wMsgID: wMsgID,
      wParam: wParam,
      wSign: md,
      wImei: 222,
      wVersion: 2,
      wRequestUserID: 4
    }
    api_js.postReq(data, (res) => {

      console.log(res, "会场列表")

      if (res.ReturnCode == 0) {

        this.setData({
          houseList2: res.Data,
        });

        console.log(this.data.houseList2, "houseList2")


      }
    });


  },


  onLoad: function () {







  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '媛月幸福汇',
      path: '/pages/index/index?isShare=1',
      imageUrl: '/images/DefaultAvatar.png',
    }
  },


})
